package com.finance.system.service;
import com.finance.system.bean.pojo.Region;
import com.finance.system.bean.pojo.RegionDetail;
import com.finance.system.dao.RegionCodeMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RegionCodeService implements IRegionCodeService{
	@Autowired
	private RegionCodeMapper regionCodeMapper;

	@Override
	@Cacheable(value="setting",cacheResolver = "settingCacheResolver",key="region_all",condition = "#result==true")
	public List<Region> listAll() {
		return regionCodeMapper.listAll();
	}

	@Override
	@Cacheable(value="setting",cacheResolver = "settingCacheResolver",key="region_pros",condition = "#result==true")
	public List<Map> getProvinces(){
		return regionCodeMapper.getProvinces();
	}

	@Override
	@Cacheable(value="setting",cacheResolver = "settingCacheResolver",key="'regioncode_'+'#code'",condition = "#result==true")
	public Region getByCode(String code) {
		return regionCodeMapper.getByCode(code);
	}

	@Override
	@Cacheable(value="setting",cacheResolver = "settingCacheResolver",key="'regiondetailcode_'+'#code'",condition = "#result==true")
	public RegionDetail getDetailByCode(String code) {
		return regionCodeMapper.getDetailByCode(code);
	}

	@Override
	@Cacheable(value="setting",cacheResolver = "settingCacheResolver",key="'region3name_'+'#provinceName'+'_'+'#cityName'+'_'+'#countyName'",condition = "#result==true")
	public Region getByName(String provinceName, String cityName, String countyName){
		if(StringUtils.isNotBlank(countyName)) {
            return this.getCounty(provinceName, cityName, countyName);
        }
		if(StringUtils.isNotBlank(cityName)) {
            return this.getCity(provinceName, cityName);
        }
 		return this.getProvince(provinceName);
	}

	@Override
	public String getCodeByName(String name) {
		return regionCodeMapper.getCodeByName(name);
	}

	@Override
	public String getCodeByNameAndParent(String name, String parent) {
		return regionCodeMapper.getCodeByNameAndParent(name,parent);
	}

    @Override
    public List<Region> provinceList() {
        return regionCodeMapper.getProvinceList();
    }


    /**
	 * 唯一名字则返回
	 * 未命中或多命中则返回null
	 * @param provinceName
	 * @param cityName
	 * @return
	 */
 	private Region getCity(String provinceName, String cityName){
		if(StringUtils.isBlank(cityName)) {
            return null;
        }
		List<Region> cityList = this.regionCodeMapper.getByNameAndLevel(cityName,2);
		if(cityList!=null&&cityList.size()==1) {
            return cityList.get(0);
        }
		if(cityList!=null&&cityList.size()>1){
			if(StringUtils.isBlank(provinceName)) {
                return null;
            }
			Region province = this.getProvince(provinceName);
			if(province!=null) {
                for (Region region : cityList) {
                    if (StringUtils.isNotBlank(region.getPcode()) && region.getPcode().equals(province.getCode())) {
                        return region;
                    }
                }
            }
		}
		return  null;
	}

	/**
	 * 唯一名字则返回
	 * 未命中或多命中则返回null
	 * @param provinceName
	 * @return
	 */
 	private Region getProvince(String provinceName){
		if(StringUtils.isBlank(provinceName)) {
            return null;
        }
		List<Region> list = this.regionCodeMapper.getByNameAndLevel(provinceName,1);
		if(list!=null&&list.size()==1) {
            return list.get(0);
        }
		return null;
	}

	/**
	 * 唯一名字则返回
	 * 未命中或多命中则返回null
	 * @param provinceName
	 * @param cityName
	 * @param countyName
	 * @return
	 */
 	private Region getCounty(String provinceName, String cityName, String countyName){
		List<Region> countyList = null;
		//直接命中
		countyList = this.regionCodeMapper.getByNameAndLevel(countyName,3);
		if(countyList!=null && countyList.size()==1) {
            return countyList.get(0);
        }
		//未命中
		if(countyList==null) {
            return null;
        }
		//多county命中 先找出命中的city待筛选
		Region city = this.getCity(provinceName,cityName);
		if(city!=null) {
            for (Region region : countyList) {
                if (StringUtils.isNotBlank(region.getPcode()) &&
                        region.getPcode().equals(city.getCode())) {
                    return region;
                }
            }
        }
		//未通过city命中 尝试通过province命中
		Region province = this.getProvince(provinceName);
		if(city!=province) {
            for (Region region : countyList) {
                if (StringUtils.isNotBlank(region.getPcode()) &&
                        region.getProvince().equals(province.getCode())) {
                    return region;
                }
            }
        }
 		return null;
	}
}
